﻿@model HouseAgencySite.Models.Areas

@{
    ViewBag.Title = "新增地区信息";
    Layout = "~/Areas/Admin/Views/Shared/_EditLayout.cshtml";
}


@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <table class="table table-bordered">
        <thead>
            <tr class="success">
                <th colspan="3" class="text-center xl">地区新增</th>
            </tr>
        </thead>
        <tr>
            <td>

                <div class="editor-label">
                    @Html.LabelFor(model => model.City)
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @{
    List<SelectListItem> list = HouseAgencySite.Utils.ParamHelper.Areas(0, "");
    List<SelectListItem> listArea = null;
    List<SelectListItem> listAddress = null;
    if (list.Count > 0)
    {
        listArea = HouseAgencySite.Utils.ParamHelper.Areas(1, list[0].Text);
    }
    if (listArea.Count > 0)
    {
        listAddress = HouseAgencySite.Utils.ParamHelper.Areas(2, listArea[0].Text);

    }
                    }

                    @Html.EditorFor(model => model.City)

                    @Html.ValidationMessageFor(model => model.City)
                </div>
            </td>
            <td>
                @if (list.Count > 0)
                {
                    @Html.DropDownListFor(model => model.City, list, new { @class = "form-control", style = "width:150px" })
                }

            </td>
        </tr>
        <tr>
            <td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Area)
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @if (listArea.Count == 1)
                    { 
                        @Html.EditorFor(model => model.Area, listArea[0].Text)
                    }
                    else
                    {
                        @Html.EditorFor(model => model.Area)
                    }

                </div>
                @Html.ValidationMessageFor(model => model.Area)
            </td>
            <td>@if (listArea.Count > 0)
                { 
                @Html.DropDownListFor(model => model.Area, listArea, new { @class = "form-control", style = "width:150px" })
                }
            </td>
        </tr>
        <tr>
            <td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.AddressName)
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.EditorFor(model => model.AddressName)

                </div>
                @Html.ValidationMessageFor(model => model.AddressName)
            </td>
            <td>@if (listAddress.Count > 0)
                { 
                @Html.DropDownListFor(model => model.AddressName, listAddress, new { @class = "form-control", style = "width:150px" })
                }
            </td>
        </tr>
        <tr>
            <td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Remark)
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.TextAreaFor(model => model.Remark)
                    @Html.ValidationMessageFor(model => model.Remark)
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.FullPath)
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.EditorFor(model => model.FullPath)
                    @Html.ValidationMessageFor(model => model.FullPath)
                </div>
            </td>
        </tr>
    </table>
    <div class="text-center alert alert-info alert-dismissabl">
        <input type="button" value="保  存" class="btn btn-primary" id="btnSubmit" />
        <input type="button" value="关  闭" class="btn btn-primary" id="btnClose" />
    </div>
}

@section Scripts {
    <script>
        $(function () {
            var changeItem = function (selId, inputId) {
                $("input[id=" + inputId + "]").val(selId.val());
            }


            ///绑定地区select函数
            var BindCountryRegion = function (sel, type, area) {
                $.ajax({
                    url: "@Url.Action("GetAreaNames")", async: false, data: { areaType: type, areaName: area }, success: function (d) {
                        $("select[id=" + sel + "]").html("");
                        if (d.length > 0) {
                            $("select[id=" + sel + "]").append("<option value=''>--请选择--</option>");
                            for (var i = 0; i < d.length; i++) {
                                $("select[id=" + sel + "]").append("<option>" + d[i].Text + "</option>");
                            }
                        }

                    }
                });
            }

            BindCountryRegion("City", 0, "");
            //市选择事件
            $("select[id=City]").change(function (event, provincevalue) {
                if ($(this).val() != "") {
                    BindCountryRegion("Area", 1, $(this).val());
                    $("Area").removeAttr("disabled");
                    $("input[id=FullPath]").val($(this).val());
                }
                if (typeof (provincevalue) != "undefined") {
                    $("#City").val(provincevalue);
                }
                changeItem($(this), "City");
            });
            //地区择事件
            $("select[id=Area]").change(function (event, cityvalue) {
                if ($(this).val() != "") {
                    BindCountryRegion("AddressName", 2, $(this).val());
                    $("AddressName").removeAttr("disabled");
                    $("input[id=FullPath]").val($("input[id=FullPath]").val() + "/" + $(this).val());
                }
                if (typeof (cityvalue) != "undefined") {
                    $("Area").val(cityvalue);
                }
                changeItem($(this), "Area")
            });

            //地区选择事件          
            $("select[id=AddressName]").change(function () {
                changeItem($(this), "AddressName")
                $("input[id=FullPath]").val($("input[id=FullPath]").val() + "/" + $(this).val());
            });
            $("input[id=AddressName]").blur(function () {
                $("input[id=FullPath]").val($("input[id=City]").val() + "/" + $("input[id=Area]").val() + "/" + $("input[id=AddressName]").val());
            });
            $("input[id=City]").blur(function () {
                $("input[id=FullPath]").val($("input[id=City]").val() + "/" + $("input[id=Area]").val() + "/" + $("input[id=AddressName]").val());
            });
            $("input[id=Area]").blur(function () {
                $("input[id=FullPath]").val($("input[id=City]").val() + "/" + $("input[id=Area]").val() + "/" + $("input[id=AddressName]").val());
            });
        })

    </script>
}
